#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/3/12 0012 9:37
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : zhe

import pandas as pd
import random as rd
import os
import matplotlib.pyplot as plt
import datetime as dt


def get_data(begin=0, end=12):
    data = pd.read_csv('chance.csv')
    data['date'] = pd.to_datetime(data['date'])
    return data[begin:end]


def make_csv(y, m):
    date = '%d/%d/1' % (y, i)
    num = '%.2f' % (rd.random() * 10)
    data = {'date': date, 'num': num}
    data = pd.DataFrame([data])
    if os.path.exists('chance.csv'):
        data.to_csv('chance.csv', index=False, sep=',', encoding='utf-8', mode='a', header=None)
    else:
        data.to_csv('chance.csv', index=False, sep=',', encoding='utf-8', mode='a')


def run1():
    y = 1970
    i = 1
    while y <= 2000:
        if i < 13:
            make_csv(y, i)
            i += 1
        else:
            i = 1
            y += 1


def run2():
    data = get_data()
    plt.plot(data['date'], data['num'])
    plt.xticks(rotation=45)
    plt.xlabel('month')  # x 说明
    plt.ylabel('test data')  # y 说明
    plt.title('this is test')
    plt.show()


def run3():
    fig = plt.figure(figsize=(10, 5))
    f1 = fig.add_subplot(2, 2, 1)
    f2 = fig.add_subplot(2, 2, 2)
    f3 = fig.add_subplot(2, 2, 3)
    f4 = fig.add_subplot(2, 2, 4)
    data = get_data()
    f1.plot(data['date'].dt.month, data['num'])
    data = get_data(13, 24)
    f2.plot(data['date'].dt.month, data['num'])
    data = get_data(25, 36)
    f3.plot(data['date'].dt.month, data['num'])
    data = get_data(37, 48)
    f4.plot(data['date'].dt.month, data['num'])
    plt.show()


def run4():
    color = ['#00FFFF', '#FFDEAD', '#6495ED', '#7FFF00', '#FF4040']
    for x in range(5):
        data = get_data(12 * x + 1, 12 * (x + 1))
        data['month'] = data['date'].dt.month
        label = 1970 + x
        plt.plot(data['month'], data['num'], c=color[x], label=label)
    plt.legend(loc='best')
    plt.xlabel('month')
    plt.ylabel('test')
    plt.title('is test')
    plt.show()


if __name__ == '__main__':
    # run1()    #建文件
    # run2()    #折线图
    # run3()    #子图
    run4()  # 一图多线
